﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using BLSwindles;

namespace Swindles.Employee
{
    public partial class EmployeeAdmin : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            grdCustomer.EditCommand += new DataGridCommandEventHandler(grdCustomer_EditCommand);
            grdCustomer.CancelCommand += new DataGridCommandEventHandler(grdCustomer_CancelCommand);
            grdCustomer.UpdateCommand += new DataGridCommandEventHandler(grdCustomer_UpdateCommand);
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCustomerList();
            }
        }

        void grdCustomer_EditCommand(object source, DataGridCommandEventArgs e)
        {
            grdCustomer.EditItemIndex = e.Item.ItemIndex;
            LoadCustomerList();
        }

        void grdCustomer_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            grdCustomer.EditItemIndex = -1;
            LoadCustomerList();
        }

        void grdCustomer_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            Control control;
            //customerID
            string customerNo = Convert.ToString(e.Item.Cells[1].Text);
            if (!string.IsNullOrEmpty(customerNo))
            {
                OBSwindles.Customer customer = BLFactory.getCustomer_ByCustomerNo(customerNo);
                //first name
                control = e.Item.Cells[2].Controls[0];
                if (control != null)
                {
                    customer.FirstName = ((TextBox)control).Text;
                }
                //last name
                control = e.Item.Cells[3].Controls[0];
                if (control != null)
                {
                    customer.LastName = ((TextBox)control).Text;
                }
                //contact number
                control = e.Item.Cells[4].Controls[0];
                if (control != null)
                {
                    customer.ContactNumber = ((TextBox)control).Text;
                }
                //contact Address
                control = e.Item.Cells[5].Controls[0];
                if (control != null)
                {
                    customer.ContactAddress = ((TextBox)control).Text;
                }
                BLFactory.updateCustomer(customer);
            }
            grdCustomer.EditItemIndex = -1;
            LoadCustomerList();
        }


        private void LoadCustomerList()
        {
            IList<OBSwindles.Customer> customerList = (IList<OBSwindles.Customer>)BLFactory.getCustomerDetails();
            grdCustomer.DataSource = customerList;
            grdCustomer.DataBind();
        }
    }
}
